package org.NS.qmuisc;



import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class PlayActivity extends Activity {
	
	private ArrayList<HashMap<String, Object>> mPlayListList = new ArrayList<HashMap<String, Object>>(); 
	private ListView mMainList;
	private SimpleAdapter mMainListAdapter;
	private Intent mListIntent;
	private MusicDBManager musicDBMan;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_play);
		
		mMainList = (ListView) findViewById(R.id.main_list);
		musicDBMan = new MusicDBManager(this);
		
		initMainList(musicDBMan);
		
		mListIntent = new Intent("android.intent.action.SINGLE_INSTANCE_SHARE");  
		mListIntent.setClassName(this, "org.NS.qmuisc.BGHandle");

		mMainList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				HashMap<String, Object> item = mPlayListList.get(arg2);
				int listID = (Integer) item.get("playListId");
				mListIntent.removeExtra("tabID");
				mListIntent.putExtra("tabID", listID);
				startActivity(mListIntent);	
			    overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left); 
			}
		});	    		
	}
	
    @Override
	protected void onResume() {
    	mPlayListList.clear();
    	HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("playListId", 0);  
        map.put("listName", getResources().getString(R.string.all_list));  
        map.put("listCount", musicDBMan.getDefaultDefaultSize()); 
        
        mPlayListList.add(map);  
        musicDBMan.getMainList(mPlayListList);
        mMainListAdapter.notifyDataSetChanged();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		musicDBMan.closeDB();
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.play, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch(item.getItemId()) {
		    case R.id.action_addPlayList:
		    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
		    	builder.setTitle(getResources().getString(R.string.addList_title))
		    	.setIcon(android.R.drawable.ic_dialog_info);
		    	
		    	final EditText input = new EditText(this);
		    	builder.setView(input)	    	
		    	.setNegativeButton(getResources().getString(R.string.cancel), null);
		    	
		    	builder.setPositiveButton(getResources().getString(R.string.confirm), new DialogInterface.OnClickListener() {
		    		@Override
		            public void onClick(DialogInterface dialog, int whichButton)
		            {
		    			String strNewListTitle = input.getText().toString();
		    			HashMap<String, Object> map = new HashMap<String, Object>();
		    	        map.put("listName", strNewListTitle);  
		    	        map.put("listCount", 0);  		    	        
		    	        
		    	        int newPlayListNo = getUnusedPlayListNo();
		    	        musicDBMan.addNewPlayList(strNewListTitle, newPlayListNo);
		    	        map.put("playListId", newPlayListNo);  
		    	        mPlayListList.add(map);  
		    	        mMainListAdapter.notifyDataSetChanged();
		            }
		    	});
		    	builder.show();
		    	break;	    
	    }
	    return true;
	}
	
	private void initMainList(MusicDBManager musicDBMan){        
        mMainListAdapter = new SimpleAdapter(this, 
        		mPlayListList,
				R.layout.main_list_item, 
				new String[] {"listName","listCount"},  
				new int[] {R.id.ListTitle,R.id.ListContentCount});
        mMainList.setAdapter(mMainListAdapter);  
	}
	
	private int getUnusedPlayListNo(){		
		int nUnUsedNo = 1;
		//because the newest list is in front
		if(mPlayListList.size() > 1){
			HashMap<String, Object> item = mPlayListList.get(1);
			nUnUsedNo = (Integer) item.get("playListId") + 1 ;
		}
		return nUnUsedNo;
	}

}
